package com.neusoft.yiyang.diet.controller;

import com.neusoft.yiyang.common.entity.diet.FoodDict;
import com.neusoft.yiyang.common.entity.diet.vto.FoodDictWithTag;
import com.neusoft.yiyang.diet.service.FoodDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "食材字典管理接口", description = "负责食材信息的增删改查及标签关联管理")
@RestController
@RequestMapping("/food-dicts")
public class FoodDictController {

    @Autowired
    private FoodDictService foodDictService;

    @Operation(summary = "添加食材", description = "新增一条食材记录到字典中")
    @PostMapping("/add")
    public void addFoodDict(
            @Parameter(description = "食材信息", required = true)
            @RequestBody FoodDict foodDict) {
        foodDictService.addFoodDict(foodDict);
    }

    @Operation(summary = "获取所有食材", description = "查询系统中所有的食材信息")
    @GetMapping("/all")
    public List<FoodDict> getAllFoodDicts() {
        return foodDictService.getAllFoodDicts();
    }

    @Operation(summary = "根据名称搜索食材", description = "通过食材名称模糊查询食材信息")
    @GetMapping("/search")
    public List<FoodDict> getFoodDictByName(
            @Parameter(description = "食材名称", required = true)
            @RequestParam String foodName) {
        return foodDictService.getFoodDictByName(foodName);
    }

    @Operation(summary = "更新食材信息", description = "修改已存在的食材信息")
    @PutMapping("/update")
    public void updateFoodDict(
            @Parameter(description = "更新后的食材信息", required = true)
            @RequestBody FoodDict foodDict) {
        foodDictService.updateFoodDict(foodDict);
    }

    @Operation(summary = "删除食材", description = "根据食材ID删除对应的食材记录")
    @DeleteMapping("/delete/{foodId}")
    public void deleteFoodDict(
            @Parameter(description = "食材ID", required = true)
            @PathVariable Integer foodId) {
        foodDictService.deleteFoodDict(foodId);
    }

    @Operation(summary = "根据标签获取食材", description = "查询包含指定饮食标签的食材列表")
    @GetMapping("get-food-with-tag-by-tagname")
    public List<FoodDictWithTag> getFoodWithTagByTag(
            @Parameter(description = "标签名称", required = true)
            @RequestParam String tagName) {
        return foodDictService.getFoodWithTagByTag(tagName);
    }

    @Operation(summary = "获取所有食材及标签", description = "查询所有食材及其关联的饮食标签")
    @GetMapping("/list-with-tags")
    public List<FoodDict> listFoodWithTags() {
        return foodDictService.listFoodWithTags();
    }
}